<template>
  <a-modal
    :open="open"
    width="700px"
    :title="rowData?.lsh ? '修改事件发生' : '新增事件发生'"
    centered
    :ok-button-props="{ disabled: !isCompleted }"
    ok-text="保存"
    @ok="onOk"
    @cancel="onCancel"
  >
    <a-form class="form-style-flex" name="validate_other" :model="formState">
      <a-form-item label="发生日期" class="w-1/2">
        <a-date-picker v-model:value="formState.triggerTime" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
      </a-form-item>
      <a-form-item label="分类" class="w-1/2" v-bind="validateInfos.eventCategory">
        <a-input v-model:value="formState.eventCategory" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="类型" class="w-1/2">
        <a-input v-model:value="formState.eventType" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="程度" class="w-1/2">
        <a-input v-model:value="formState.extent" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="范围" class="w-full">
        <a-input v-model:value="formState.range" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="症候" class="w-full">
        <a-input v-model:value="formState.symptom" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="侦检" class="w-full">
        <a-input v-model:value="formState.detection" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="组织指挥" class="w-full">
        <a-input v-model:value="formState.command" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="处置措施" class="w-full">
        <a-input v-model:value="formState.measure" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="控制方法" class="w-full">
        <a-input v-model:value="formState.method" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :rows="5" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import dayjs from 'dayjs'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'update:value', 'success'])

const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const formState = ref({
  eventCategory: '',
  triggerTime: dayjs().format('YYYY-MM-DD')
})
const rulesState = ref({
  eventCategory: [{ required: true, message: '请输入分类' }]
})
const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  const { code, msg } = await post('/war/epidemicHygieneProtection/doSaveProtectionEvent', formState.value)
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('update:open', false)
  emit('success')
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      if (props.rowData?.id) {
        formState.value = { ...props.rowData }
      } else {
        formState.value = { ...props.rowData, triggerTime: dayjs().format('YYYY-MM-DD') }
      }
    }
  }
)
</script>
<style lang="less" scoped></style>
